/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaAutenticacion.java
 *
 * Created on 9/04/2011, 02:28:05 PM
 */
package frontera.interfaz.ventana;

import control.SIRHApp;
import control.ValidarAutenticacion;
import entidad.Usuario;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 *
 * @author Sebastian
 */
public class VentanaAutenticacion2 extends javax.swing.JFrame {

    /** Creates new form VentanaAutenticacion */
    public VentanaAutenticacion2() {
        initComponents();

        java.net.URL url = ClassLoader.getSystemResource("frontera/recursos/Changepass.png");
        Toolkit kit = Toolkit.getDefaultToolkit();
        java.awt.Image img = kit.createImage(url);
        this.setIconImage(img);
        panelPrincipal.remove(labelResultado);

        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelResultado = new javax.swing.JLabel();
        botonIngresar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        panelPrincipal = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        fieldUsuario = new javax.swing.JTextField();
        labelUsuario = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        fieldContraseña = new javax.swing.JPasswordField();
        labelContrasena = new javax.swing.JLabel();

        labelResultado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/warning.png"))); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Autenticacion");
        setResizable(false);

        botonIngresar.setText("Ingresar");
        botonIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonIngresarActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/logo_Transparente.png"))); // NOI18N

        jButton1.setText("Salir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        panelPrincipal.setLayout(new java.awt.GridLayout(2, 1));

        fieldUsuario.setText("GerenteGral");
        fieldUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fieldUsuarioActionPerformed(evt);
            }
        });
        fieldUsuario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                fieldUsuarioFocusGained(evt);
            }
        });

        labelUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/user.png"))); // NOI18N
        labelUsuario.setText("Usuario");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fieldUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelUsuario)
                    .addComponent(fieldUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        panelPrincipal.add(jPanel2);

        fieldContraseña.setText("11111111");
        fieldContraseña.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fieldContraseñaActionPerformed(evt);
            }
        });
        fieldContraseña.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                fieldContraseñaFocusGained(evt);
            }
        });

        labelContrasena.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/password.png"))); // NOI18N
        labelContrasena.setText("Contraseña");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelContrasena, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(fieldContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelContrasena)
                    .addComponent(fieldContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        panelPrincipal.add(jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(panelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 173, Short.MAX_VALUE)
                                .addComponent(botonIngresar))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 173, Short.MAX_VALUE)
                .addComponent(botonIngresar)
                .addGap(22, 22, 22))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonIngresar, jButton1});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(panelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(botonIngresar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonIngresarActionPerformed

        usuario = new Usuario(fieldUsuario.getText(), String.valueOf(fieldContraseña.getPassword()), null);
        validacion = new ValidarAutenticacion(SIRHApp.getInstance().getSistema());

        boolean acceso = validacion.ValidarDatos(usuario);

        if (acceso == true) {
            this.setVisible(false);
            SIRHApp.getInstance().setVentanaPrincipal(new VentanaPrincipal());
            SIRHApp.getInstance().getVentanaPrincipal().setVisible(true);
            SIRHApp.getInstance().getVentanaPrincipal().getPestanasPrincipal().setSelectedIndex(0);
            this.dispose();
        } else {
            panelPrincipal.setVisible(false);
            panelPrincipal.setLayout(new GridLayout(3, 1));
            panelPrincipal.add(labelResultado);
            panelPrincipal.setVisible(true);
            labelResultado.setText("Datos Incorrectos");
            error = true;
        }
}//GEN-LAST:event_botonIngresarActionPerformed

    private void fieldContraseñaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fieldContraseñaActionPerformed
        // Cuando de ENTER en el campo de contraseña
        botonIngresarActionPerformed(evt);
    }//GEN-LAST:event_fieldContraseñaActionPerformed

    private void fieldUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fieldUsuarioActionPerformed
        // Cuando de ENTER en el campo de usuario
        botonIngresarActionPerformed(evt);
    }//GEN-LAST:event_fieldUsuarioActionPerformed

    private void fieldContraseñaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_fieldContraseñaFocusGained
        //Si el campo ya tenía algo borrelo
        if (error) {
            panelPrincipal.setVisible(false);
            panelPrincipal.remove(labelResultado);
            panelPrincipal.setLayout(new GridLayout(2, 1));
            panelPrincipal.revalidate();
            panelPrincipal.setVisible(true);
            error = false;
        }
        if (!(String.valueOf(fieldContraseña.getPassword()).isEmpty())) {
            fieldContraseña.setText("");
            fieldContraseña.requestFocus();
        }
    }//GEN-LAST:event_fieldContraseñaFocusGained

    private void fieldUsuarioFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_fieldUsuarioFocusGained
        if (error) {
            panelPrincipal.setVisible(false);
            panelPrincipal.remove(labelResultado);
            panelPrincipal.setLayout(new GridLayout(2, 1));
            panelPrincipal.setVisible(true);
            error = false;
        }
        if (!(fieldUsuario.getText().isEmpty())) {
            fieldUsuario.selectAll();
            fieldUsuario.requestFocus();
        }
    }//GEN-LAST:event_fieldUsuarioFocusGained

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VentanaAutenticacion2().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonIngresar;
    private javax.swing.JPasswordField fieldContraseña;
    private javax.swing.JTextField fieldUsuario;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel labelContrasena;
    private javax.swing.JLabel labelResultado;
    private javax.swing.JLabel labelUsuario;
    private javax.swing.JPanel panelPrincipal;
    // End of variables declaration//GEN-END:variables
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Usuario usuario;
    
    private ValidarAutenticacion validacion;
    private boolean error = false;
}
